package com.xingkeduo.delayQueue.service;


import com.xingkeduo.delayQueue.model.Task;
import com.xingkeduo.delayQueue.model.TaskModel;
import com.xingkeduo.delayQueue.model.TaskModelEnum;
import com.xingkeduo.utils.SpringContextHolder;

import java.lang.reflect.Method;

public interface TaskService {

    //入队
    void addToDeplayQueue(TaskModel task);

    //删除
    boolean removeFromDeplayQueue(TaskModel task);


    default void addToTaskQue(String key, String value) {
        try {
            Task task = new Task(key,value);
            TaskModel taskModel = TaskModel.covertToTaskModel(task);
            String id = taskModel.getId();
            TaskModelEnum taskModelEnum = taskModel.getTaskModelEnum();
            Object bean = SpringContextHolder.getApplicationContext().getBean(taskModelEnum.getClassName());
            Method declaredMethod = bean.getClass().getDeclaredMethod(taskModelEnum.getMethodName(), String.class);
            declaredMethod.invoke(bean, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
